﻿using Moven.Data;
using Moven.ViewModels;
using PCLStorage;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using System.Threading.Tasks;
using System;

#if __ANDROID__
using Android.App;
using Android.Widget;
using Plugin.CurrentActivity;
using AndroidHUD;
#elif __IOS__
using MaterialControls;
using BigTed;
using static BigTed.ProgressHUD;
#endif

namespace Moven
{
	static class Statics
    {
        public const string AppName = "墨文";

		public const string ArticleChangedMessageId = "ArticleChanged";

#if __ANDROID__
		public static Activity CurrentActivity => CrossCurrentActivity.Current.Activity;
#endif

		static ArticlesStore _articlesStore;
        public static ArticlesStore ArticlesStore => _articlesStore;

        public static void Init(IFolder folder = null)
        {
			_articlesStore = new ArticlesStore(new LocalKeyValueStorage(folder), new CloudKeyValueStorage());
        }

		public static List<ArticleWithBookmark> GetBookmarkedArticles(List<int> bookmarks = null)
        {
            var articlesDict = ArticlesStore.GetArticlesAsync().Result.Articles.ToDictionary(x => x.Index);
			bookmarks = bookmarks?? ArticlesStore.GetBookmarksAsync().Result;
            var bookmarkedArticles = bookmarks.
                    Select(x => ArticleWithBookmark.FromArticle(articlesDict[x])).ToList();
            bookmarkedArticles.ForEach(x => x.IsBookmarked = true);
            return bookmarkedArticles;
        }

        public static async Task RemoveArticleFromBookmarks(ArticleWithBookmark article)
        {
            var bookmarks = await ArticlesStore.GetBookmarksAsync();
            bookmarks.Remove(article.Index);
			await ArticlesStore.SaveBookmarksAsync(bookmarks);
        }

		public static async Task AddArticleToBookmarks(ArticleWithBookmark article)
		{
			var bookmarks = await ArticlesStore.GetBookmarksAsync();
			bookmarks.Insert(0, article.Index);
			await ArticlesStore.SaveBookmarksAsync(bookmarks);
		}

		public static async Task SaveArticleBookmarkStatus(ArticleWithBookmark article)
		{
			var operation = new BookmarkOperation 
			{ 
				Indices = new []{ article.Index }, 
				Action = article.IsBookmarked? BookmarkAction.Add : BookmarkAction.Remove 
			};
			if (article.IsBookmarked)
			{
				await AddArticleToBookmarks(article);
			}
			else
			{
				await RemoveArticleFromBookmarks(article);
			}
			await ArticlesStore.AddBookmarkOperationAsync(operation);
		}

		public static void SetBackButtonTitle(this BindableObject page, string title = "返回")
		{
			NavigationPage.SetBackButtonTitle(page, title);
		}

		static Color _separatorColor = Color.Default;
		public static Color SeparatorColor =>
			_separatorColor == Color.Default? 
		                        (_separatorColor = GetResource<Color>("SeparatorColor")) : _separatorColor;

        public static void ShowToast(string message)
        {
#if __ANDROID__
			Toast.MakeText(CurrentActivity, message, ToastLength.Short).Show();
#elif __IOS__
			var toast = new MDToast(message, MDToast.DurationShort);
			toast.Show();
#endif
        }

		public static T GetResource<T>(string index)
		{
			return (T)Xamarin.Forms.Application.Current.Resources[index];
		}

		static readonly TimeSpan HUDTimeout = TimeSpan.FromSeconds(2);
#if __ANDROID__

		public static void ShowHUD(string status)
		{
			AndHUD.Shared.Show(CurrentActivity, status, maskType: MaskType.Black);
		}

		public static void ShowSuccessHUDWithStatus(string status)
		{
			AndHUD.Shared.ShowSuccessWithStatus(CurrentActivity, status, MaskType.Black, HUDTimeout);
		}

		public static void ShowErrorHUDWithStatus(string status)
		{
			AndHUD.Shared.ShowErrorWithStatus(CurrentActivity, status, MaskType.Black, HUDTimeout);
		}

#elif __IOS__

		public static void ShowHUD(string status)
		{
			BTProgressHUD.Show(status, maskType: MaskType.Black);
		}

		public static void ShowSuccessHUDWithStatus(string status)
		{
			BTProgressHUD.ShowSuccessWithStatus(status, HUDTimeout.TotalMilliseconds);
		}

		public static void ShowErrorHUDWithStatus(string status)
		{
			BTProgressHUD.ShowErrorWithStatus(status, HUDTimeout.TotalMilliseconds);
		}

#endif
    }
}
